package com.atguigu.controller;

import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.result.Result;
import com.atguigu.service.OaProcessService;
import com.atguigu.service.OaProcessTemplateService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.Multipart;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "审批模板管理")
@RestController
@RequestMapping("/admin/process/processTemplate")
public class OaProcessTemplateController {
    @Autowired
    private OaProcessTemplateService oaProcessTemplateService;
    @Autowired
    private OaProcessService oaProcessService;
    @ApiOperation("分页查询")
    @GetMapping("/{page}/{limit}")
    public Result getPage(@PathVariable Integer page,@PathVariable Integer limit){
        IPage<ProcessTemplate> page1=new Page<>(page,limit);
        IPage<ProcessTemplate> page2=oaProcessTemplateService.selectPage(page1);
        return Result.ok(page2);
    }
    @ApiOperation("添加")
    @PostMapping("save")
    public Result save(@RequestBody ProcessTemplate processTemplate){
        oaProcessTemplateService.save(processTemplate);
        return Result.ok();
    }
    @ApiOperation("删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Integer id){
        oaProcessTemplateService.removeById(id);
        return Result.ok();
    }
    @ApiOperation("根据id获取")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Integer id){
        ProcessTemplate oaProcessTemplateServiceById = oaProcessTemplateService.getById(id);
        return Result.ok(oaProcessTemplateServiceById);
    }
    @ApiOperation("修改")
    @PutMapping("update")
    public Result update(@RequestBody ProcessTemplate processTemplate){
        oaProcessTemplateService.updateById(processTemplate);
        return Result.ok();
    }
    @ApiOperation("上传流程")
    @PostMapping("upload")
    public Result upload(MultipartFile file) throws IOException {
        String path = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath()+"process/";
        File file1 = new File(path);
        String originalFilename = file.getOriginalFilename();
        if (!file1.exists()){
            file1.mkdirs();
        }
        File file2 = new File(path + originalFilename);
        //将文件保存到指定文件路径
        file.transferTo(file2);

        Map<String,Object> map=new HashMap<>();
        map.put("processDefinitionKey",originalFilename.substring(0,originalFilename.lastIndexOf(".")));
        map.put("processDefinitionPath",file2.getAbsolutePath());
        return Result.ok(map);

    }
    @ApiOperation("发布")
    @GetMapping("publish/{id}")
    public Result publish(@PathVariable Integer id){

        ProcessTemplate processTemplate = oaProcessTemplateService.getById(id);
        processTemplate.setStatus(1);
        oaProcessTemplateService.updateById(processTemplate);
        //部署流程定义
        if (!StringUtils.isEmpty(processTemplate.getProcessDefinitionPath())){
            oaProcessService.deployByZip(processTemplate.getProcessDefinitionPath());
        }
        return Result.ok();
    }


}
